//
// Created by hp on 2019-12-18.
//

#include "Flight.h"
#include "Bullet.h"
Flight::Flight(vector<string> paths, string expSound,point point1, bool is,int w, int h,int v,int hp):Gameobject(paths,expSound,point1,w,h,v) {
    bullet=50;
    is_enemy=is;
    bulType=1;
    HP=10;
    enemyDir='S';
    HP=hp;
    startTime=rand()%20+3;
    count=0;
}
Flight::~Flight() {}

Bullet* Flight::shoot() {
    string sound;
    if(is_enemy){
        sound=" ";
    } else{
        if(bulType==1) sound="res/explosion1.mp3";
        else if(bulType==2) sound="res/explosion2.mp3";
        else if(bulType==3) sound="res/explosion3.mp3";
    }
    if(bulType==1){
        Bullet* bul= new Bullet({"res/bullet1.png","res/explosion1-1.png","res/explosion1-2.png","res/explosion1-3.png"},sound,{pos.x + wid / 2, pos.y + hei/4},
                                is_enemy,1,30,40,500);
        return bul;
    } else if(bulType==2){
        Bullet* bul= new Bullet({"res/bullet2.png","res/explosion2-1.png","res/explosion2-2.png","res/explosion2-3.png"},sound,{pos.x + wid / 2, pos.y + hei/4},
                                is_enemy,2,30,40,500);
        return bul;
    } else if(bulType==3){
        Bullet* bul=new Bullet({"res/bullet3.png","res/explosion3-1.png","res/explosion3-2.png","res/explosion3-3.png"},sound,{pos.x + wid / 2, pos.y + hei/4},
                               is_enemy,3,30,40,500);
        return bul;
    } else{
        return nullptr;
}
}

void Flight::changeBullet() {
    if (bulType != 3) {
        bulType++;
    } else {
        bulType = 1;
    }
}

char Flight::randDir(){
    int d;
    char dir;
    if(pos.y>=Game::Height/2) {
        d=rand()%4;
        switch (d) {
            case 0:
                dir = 'A';
                break;
            case 1:
                dir = 'S';
                break;
            case 2:
                dir = 'D';
                break;
            case 3:
                dir='W';
        }
    } else{
        d=rand()%3;
        switch (d){
            case 0:
                dir='A';
                break;
            case 1:
                dir='D';
                break;
            case 2:
                dir='W';
        }
    }
    if(pos.x<=0){
        dir='D';
    }
    if(pos.x>=Game::Width-wid){
        dir='A';
    }
    if(pos.y>=Game::Height-hei){
        dir='S';
    }
    return dir;
}

void Flight::move(char dir) {
    float dis=volocity;
    switch (dir) {
        case 'W':
            if(pos.y<Game::Height-hei) {
                pos.y += dis;
            }else{
                pos.y=Game::Height-hei;
            }
            break;
        case 'S':
            if(pos.y>0) {
                pos.y -= dis;
            } else{
                pos.y=0;
            }
            break;
        case 'D':
            if(pos.x<Game::Width-wid) {
                pos.x += dis;
            } else{
                pos.x=Game::Width-wid;
            }
            break;
        case 'A':
            if(pos.x>0) {
                pos.x -= dis;
            } else{
                pos.x=0;
            }
    }
}